package com.yoomiito.app.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.utils.ResUtil;

import butterknife.Unbinder;
import cn.droidlover.xdroidmvp.log.XLog;

public class CustomDialog extends Dialog implements View.OnClickListener {

    //    @BindView(R.id.tv_title)
    TextView tv_title;
    //    @BindView(R.id.iv_head)
    ImageView iv_head;
    //    @BindView(R.id.tv_content)
    TextView tv_content;
    //    @BindView(R.id.cancel)
    Button cancel;
    //    @BindView(R.id.confirm)
    Button confirm;

    private Context mContext;
    private Unbinder ub;
    private OnCloseListener mOnCloseListener;
    private String title;
    private String content;
    private String url;
    private int size = -1;
    private int color = -1;
    private TextView tv_content_1;
    private String content_1;

    public CustomDialog(@NonNull Context context, OnCloseListener onCloseListener) {
        super(context, R.style.dialog);
        mContext = context;
        mOnCloseListener = onCloseListener;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        View view = LayoutInflater.from(mContext).inflate(R.layout.coustom_dialog, null);
//        ub = ButterKnife.bind(this, view);
        tv_content = view.findViewById(R.id.tv_content);
        tv_content_1 = view.findViewById(R.id.tv_content_1);
        iv_head = view.findViewById(R.id.iv_head);
        tv_title = view.findViewById(R.id.tv_title);

        view.findViewById(R.id.cancel).setOnClickListener(this);
        view.findViewById(R.id.confirm).setOnClickListener(this);
        setContentView(view);
        setCanceledOnTouchOutside(false);

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = (int) (App.mWidthPx*0.72);
        getWindow().setAttributes(lp);

        tv_title.setText(title);
        tv_content.setText(content);
        if (!TextUtils.isEmpty(content_1)){
            tv_content_1.setText(content_1);
            tv_content_1.setVisibility(View.VISIBLE);
        }
        if (size != -1) {
            tv_content.setTextSize(size);
        }
        if (color != -1) {
            tv_content.setTextColor(color);
        }
        if (url != null) {
            iv_head.setVisibility(View.VISIBLE);
            Glide.with(mContext)
                    .load(url)
                    .into(iv_head);
        } else {
            iv_head.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    public void setTitle(String str) {
        this.title = str;
    }

    public void setContent(String str) {
        if (tv_content != null) {
            tv_content.setText(str);
        }
        this.content = str;
    }

    public void setContent_1(String str){
        if (tv_content_1!=null){
            tv_content_1.setText(str);
        }
        this.content_1 = str;
    }

    public void setContentSize(int size) {
        this.size = size;
    }

    public void setContentColor(int color) {
        this.color = ResUtil.getColor(color);
    }

    public void setHead(String url) {
        if (iv_head!=null&&url != null) {
            iv_head.setVisibility(View.VISIBLE);
            Glide.with(mContext)
                    .load(url)
                    .into(iv_head);
        }
        this.url = url;
    }

//    @OnClick({R.id.cancel, R.id.confirm})
//    @Override
//    public void onViewClicked(View view) {
//
//    }

    @Override
    protected void onStop() {
        super.onStop();
//        ub.unbind();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                dismiss();
                break;
            case R.id.confirm:
                if (mOnCloseListener != null) {
                    mOnCloseListener.confirm();
                } else {
                    XLog.e("Dialog", "没有设置mOnCloseListener");
                }
                dismiss();
                break;
        }
    }

    public interface OnCloseListener {
        void confirm();
    }
}
